/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EditarProducto.java
 *
 * Created on 19/05/2010, 11:18:00 PM
 */

package ui;

import acceso.Proveedor;
import gestion.GestionSpring;
import interfaz.InterfaceEjp;
import javax.swing.JOptionPane;
import org.apache.log4j.Logger;

/**
 *
 * @author Adriano
 */
public class EditarProveedor extends javax.swing.JFrame {

    private static Logger logger = Logger.getLogger(EditarProveedor.class);
    Long Codigo;
    PaginaPrincipal miPaginaPrincipal;

/**
 * Setea la pagina principal.
 * @param miPaginaPrincipal
 */
    public void setMiPaginaPrincipal(PaginaPrincipal miPaginaPrincipal) {
        this.miPaginaPrincipal = miPaginaPrincipal;
        nombreProveedor.setEditable(false);
        tipoProveedor.setEnabled(false);
        direccionProveedor.setEditable(false);
        nombreContactoProveedor.setEditable(false);
        numeroContactoProveedor.setEditable(false);
        codigoArea.setEditable(false);
        rifProveedor.setEditable(false);
       //llamada para llenar los datos de modificación.
        llenarDatos();

    }
    /**
     * Matodo para llenar los datos a modificar.
     */
        public void llenarDatos(){

         InterfaceEjp miInterfaceEjp = GestionSpring.getSingleton()
         .MetodoGestion();

         Proveedor proveedor = new Proveedor();
         proveedor = (Proveedor) miInterfaceEjp.BuscarProveedor(Codigo);
         rifProveedor.setText(proveedor.getRif());
         nombreProveedor.setText(proveedor.getNombre());
         direccionProveedor.setText(proveedor.getDireccion());
         nombreContactoProveedor.setText(proveedor.getNombreContacto());
         codigoArea.setText(proveedor.getNumeroContacto().substring(0, 4));
         numeroContactoProveedor.setText(proveedor.getNumeroContacto()
         .substring(5, 12));

            if(proveedor.getTipo().equals("P")){
                    tipoProveedor.setSelectedIndex(1);
                }else{
                    tipoProveedor.setSelectedIndex(2);
                }
        }

       /**
        * Metodo para setear el codigo del proveedor a modificar.
        * @param Codigo
        */
        public void setCodigo(Long Codigo) {
        this.Codigo = Codigo;
        System.out.println("Codigo = " + Codigo);
       }


    /** Creates new form EditarProducto */
    public EditarProveedor() {
        initComponents();
           
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        nombreProveedor = new javax.swing.JTextField();
        tipoProveedor = new javax.swing.JComboBox();
        direccionProveedor = new javax.swing.JTextField();
        nombreContactoProveedor = new javax.swing.JTextField();
        numeroContactoProveedor = new javax.swing.JTextField();
        botonAceptar = new javax.swing.JButton();
        botonCancelar = new javax.swing.JButton();
        NOMBRE = new javax.swing.JRadioButton();
        TIPO = new javax.swing.JRadioButton();
        DIRECCION = new javax.swing.JRadioButton();
        TEL1 = new javax.swing.JRadioButton();
        TEL2 = new javax.swing.JRadioButton();
        codigoArea = new javax.swing.JTextField();
        rifProveedor = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Inversiones E.J.P");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Editar Proveedor"));
        jPanel1.setName("jPanel1"); // NOI18N

        nombreProveedor.setName("nombreProveedor"); // NOI18N

        tipoProveedor.setModel(new javax.swing.DefaultComboBoxModel(new String[] {"Seleccione", "Papeleria", "Limpieza" }));
        tipoProveedor.setName("tipoProveedor"); // NOI18N

        direccionProveedor.setName("direccionProveedor"); // NOI18N

        nombreContactoProveedor.setName("nombreContactoProveedor"); // NOI18N

        numeroContactoProveedor.setName("numeroContactoProveedor"); // NOI18N
        numeroContactoProveedor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                numeroContactoProveedorActionPerformed(evt);
            }
        });

        botonAceptar.setText("Aceptar");
        botonAceptar.setName("botonAceptar"); // NOI18N
        botonAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonAceptarActionPerformed(evt);
            }
        });

        botonCancelar.setText("Cancelar");
        botonCancelar.setName("botonCancelar"); // NOI18N
        botonCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonCancelarActionPerformed(evt);
            }
        });

        NOMBRE.setText("Nombre:");
        NOMBRE.setName("NOMBRE"); // NOI18N
        NOMBRE.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                NOMBREActionPerformed(evt);
            }
        });

        TIPO.setText("Tipo:");
        TIPO.setName("TIPO"); // NOI18N
        TIPO.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TIPOActionPerformed(evt);
            }
        });

        DIRECCION.setText("Dirección");
        DIRECCION.setName("DIRECCION"); // NOI18N
        DIRECCION.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DIRECCIONActionPerformed(evt);
            }
        });

        TEL1.setText("Nombre Contacto");
        TEL1.setName("TEL1"); // NOI18N
        TEL1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TEL1ActionPerformed(evt);
            }
        });

        TEL2.setText("Telefono Contacto");
        TEL2.setName("TEL2"); // NOI18N
        TEL2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TEL2ActionPerformed(evt);
            }
        });

        codigoArea.setName("codigoArea"); // NOI18N

        rifProveedor.setName("rifProveedor"); // NOI18N

        jLabel1.setText("RIF");
        jLabel1.setName("jLabel1"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(TEL1))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(TEL2))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(TIPO))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(NOMBRE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(DIRECCION))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(28, 28, 28)
                        .addComponent(jLabel1)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                            .addComponent(codigoArea, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(numeroContactoProveedor, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(nombreContactoProveedor, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(rifProveedor, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(nombreProveedor, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(tipoProveedor, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(direccionProveedor, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 187, Short.MAX_VALUE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(botonAceptar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(botonCancelar)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rifProveedor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nombreProveedor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(NOMBRE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tipoProveedor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(TIPO))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(direccionProveedor, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(52, 52, 52)
                        .addComponent(DIRECCION)))
                .addGap(28, 28, 28)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(TEL1)
                    .addComponent(nombreContactoProveedor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(TEL2)
                    .addComponent(codigoArea, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(numeroContactoProveedor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botonCancelar)
                    .addComponent(botonAceptar))
                .addGap(27, 27, 27))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-368)/2, (screenSize.height-487)/2, 368, 487);
    }// </editor-fold>//GEN-END:initComponents

    private void botonAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonAceptarActionPerformed

      try{
      //se validan las excepciones.
        manejoExcepciones.ExcepcionesProveedor.
        CamposNoLlenos(nombreProveedor.getText(),
        tipoProveedor.getSelectedIndex(), direccionProveedor.getText(),
        nombreContactoProveedor.getText(), numeroContactoProveedor.getText());

        Integer.parseInt(numeroContactoProveedor.getText());
        Integer.parseInt(codigoArea.getText());
        manejoExcepciones.ExcepcionesProveedor.
        validaNumeroTlf(numeroContactoProveedor.getText());

        manejoExcepciones.ExcepcionesProveedor.
        validaCodigoArea(codigoArea.getText());

       //Se crea el objeto proveedor que se quiere guardar.
        Proveedor proveedor =  new Proveedor();
        //se setean los valores al objeto.
        proveedor.setIdProveedor(Codigo);
        proveedor.setDireccion(direccionProveedor.getText());
        proveedor.setNombre(nombreProveedor.getText());
        proveedor.setNombreContacto(nombreContactoProveedor.getText());
        String numeroCon =codigoArea.getText()+"-"
        +numeroContactoProveedor.getText()+"";

        proveedor.setNumeroContacto(numeroCon);
        proveedor.setTipo(tipoProveedor.getSelectedItem().toString()
        .substring(0, 1));

        proveedor.setRif(rifProveedor.getText());
      //se instancia para obtener la interfaz de gestion.
        InterfaceEjp miInterfaceEjp = GestionSpring.getSingleton().MetodoGestion();
        miInterfaceEjp.registrarProveedor(proveedor);
       //se guarda en la bitacora
        logger.info("Modificacion del provedor con codigo: " + Codigo);
        //se actualiza la pagina principal.
        miPaginaPrincipal.Refrescar();
        this.dispose();
        }catch(manejoExcepciones.Excepciones e){
             logger.info("A ocurrido un error detectado por una excepcion");
             JOptionPane.showMessageDialog(this,e.toString().substring(30, 
             e.toString().length()));

         }catch (NumberFormatException NFE){
             logger.info("A ocurrido un error con el campo Numero de Contacto");
             JOptionPane.showMessageDialog(this,"El valor de Numero de " +
                     "Contacto deben ser numeros");
         }
}//GEN-LAST:event_botonAceptarActionPerformed

    private void botonCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonCancelarActionPerformed
        logger.info("Salio de la pagina Editar Proveedor");
        this.dispose();
    }//GEN-LAST:event_botonCancelarActionPerformed

    private void NOMBREActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_NOMBREActionPerformed
        //si selecciona el radio button se activa el campo si se deselecciona se desactiva
        if(NOMBRE.isSelected()==true)
            nombreProveedor.setEditable(true);
        else
            nombreProveedor.setEditable(false);

}//GEN-LAST:event_NOMBREActionPerformed

    private void TIPOActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TIPOActionPerformed
        //si selecciona el radio button se activa el campo si se deselecciona se desactiva
        if(TIPO.isSelected()==true)
            tipoProveedor.setEnabled(true);
        else
            tipoProveedor.setEnabled(false);
}//GEN-LAST:event_TIPOActionPerformed

    private void DIRECCIONActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DIRECCIONActionPerformed
        //si selecciona el radio button se activa el campo si se deselecciona se desactiva
        if(DIRECCION.isSelected()==true)
            direccionProveedor.setEditable(true);
        else
            direccionProveedor.setEditable(false);
}//GEN-LAST:event_DIRECCIONActionPerformed

    private void TEL1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TEL1ActionPerformed
       //si selecciona el radio button se activa el campo si se deselecciona se desactiva
        if(TEL1.isSelected()==true)
            nombreContactoProveedor.setEditable(true);
        else
            nombreContactoProveedor.setEditable(false);
}//GEN-LAST:event_TEL1ActionPerformed

    private void TEL2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TEL2ActionPerformed
        //si selecciona el radio button se activa el campo si se deselecciona se desactiva
        if(TEL2.isSelected()==true){
            codigoArea.setEditable(true);
            numeroContactoProveedor.setEditable(true);
        }else{
            codigoArea.setEditable(false);
            numeroContactoProveedor.setEditable(false);
        }
}//GEN-LAST:event_TEL2ActionPerformed

    private void numeroContactoProveedorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_numeroContactoProveedorActionPerformed
        
    }//GEN-LAST:event_numeroContactoProveedorActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new EditarProveedor().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JRadioButton DIRECCION;
    private javax.swing.JRadioButton NOMBRE;
    private javax.swing.JRadioButton TEL1;
    private javax.swing.JRadioButton TEL2;
    private javax.swing.JRadioButton TIPO;
    private javax.swing.JButton botonAceptar;
    private javax.swing.JButton botonCancelar;
    private javax.swing.JTextField codigoArea;
    private javax.swing.JTextField direccionProveedor;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField nombreContactoProveedor;
    private javax.swing.JTextField nombreProveedor;
    private javax.swing.JTextField numeroContactoProveedor;
    private javax.swing.JTextField rifProveedor;
    private javax.swing.JComboBox tipoProveedor;
    // End of variables declaration//GEN-END:variables

}
